package evol;

import java.util.Random;

import ia.battle.camp.Warrior;
import ia.battle.camp.WarriorManager;
import ia.exceptions.RuleException;

public class ManagerEvol extends WarriorManager {
	private Random r = new Random();
	private Warrior w;

	public Warrior getW() {
		return w;
	}

	public ManagerEvol(Warrior winner) {
		this.w = winner;
	}

	@Override
	public Warrior getNextWarrior() throws RuleException {
		if (w == null) {
			w = parametrizar();
			return w;
		} else {
			//w = aparear(w);
			w= new WarriorJ(w.getName(),w.getInitialHealth(),w.getInitialDefense(),w.getInitialStrength(),w.getInitialSpeed(),w.getInitialRange());
			return w;
					
		}
	}
	public void changeWarrior(Warrior winner){
		try {
			w = aparear(winner);
		} catch (RuleException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "JoaquinPega";
	}

	private Warrior parametrizar() throws RuleException{

		return new WarriorJ("Bot_"+this.hashCode(),20,20,20,20,20);
	}
	private Warrior aparear(Warrior winner) throws RuleException{
		int h,d,str,sp,rng,sw;
		sw = r.nextInt(20);
		//System.out.println(sw);
		switch(sw){
		case 0:	//rango en funcion de speed
			sp=-1;
			h = winner.getInitialHealth();
			d = winner.getInitialDefense();
			str = winner.getInitialStrength();
			while(sp<=4){
				sp = winner.getInitialSpeed() - r.nextInt(20)*5;
			}			
			rng = 100 -h -d -str- sp;
			break;
		case 1:	//defensa en funcion de vida
			h = -1;
			sp = winner.getInitialSpeed();
			str = winner.getInitialStrength();
			rng = winner.getInitialRange();
			while(h <=0){
					h = winner.getInitialHealth() - r.nextInt(100);
			}
			d = 100 - sp -h-str -rng;
			break;
		case 2://defensa en funcion de ataque
			str = -1;
			sp = winner.getInitialSpeed();
			h = winner.getInitialHealth();
			rng = winner.getInitialRange();
			while(str <=0){
				str = winner.getInitialStrength()- r.nextInt(100);
			}
			d = 100 - sp-h-str-rng;
			
			break;
		case 3://vida en funcion de ataque
			str = -1;
			sp = winner.getInitialSpeed();
			d = winner.getInitialDefense();
			rng = winner.getInitialRange();
			while(str <=0){
				str = winner.getInitialStrength()- r.nextInt(100);
			}
			h = 100 - sp-d-str-rng;
			break;
		case 4://ataque en funcion de defensa
			d = -1;
			sp = winner.getInitialSpeed();
			h = winner.getInitialHealth();
			rng = winner.getInitialRange();
			while(d <=0){
				d = winner.getInitialDefense()- r.nextInt(100);
			}
			str = 100 - sp-h-d-rng;
			break;
		case 5:// rango en funcion de vida
			h = -1;
			sp = winner.getInitialSpeed();
			str = winner.getInitialStrength();
			d = winner.getInitialDefense();
			while(h <=0){
				h = winner.getInitialHealth() - r.nextInt(100);
			}
			rng = 100 -h -d -str- sp;
			break;
		case 6://rango en funcion de strenght
			str = -1;
			sp = winner.getInitialSpeed();
			d = winner.getInitialDefense();
			h = winner.getInitialHealth();
			while(str <=0){
				str = winner.getInitialStrength()- r.nextInt(100);
			}
			rng = 100 -h -d -str- sp;
			break;
		case 7://rango en funcion de defensa
			d = -1;
			sp = winner.getInitialSpeed();
			h = winner.getInitialHealth();
			str = winner.getInitialStrength();
			while(d <=0){
				d = winner.getInitialDefense()- r.nextInt(100);
			}
			rng = 100 -h -d -str- sp;
			break;
		case 8://defensa en funcion de speed
			sp=-1;
			h = winner.getInitialHealth();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			while(sp<=4){
				sp = winner.getInitialSpeed() - r.nextInt(20)*5;
			}	
			d = 100 - sp-h-str-rng;
			break;
		case 9://defensa en funcion de rango
			rng = -1;
			sp = winner.getInitialSpeed();
			h = winner.getInitialHealth();
			str = winner.getInitialStrength();
			while(rng<=0){
				rng = winner.getInitialRange() - r.nextInt(100);
			}	
			d = 100 - sp-h-str-rng;
			break;
		case 10://vida en funcion de defensa
			d = -1;
			sp = winner.getInitialSpeed();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			while(d <=0){
				d = winner.getInitialDefense()- r.nextInt(100);
			}
			h = 100 - sp-d-str-rng;
			break;
		case 11://vida en funcion de speed
			sp=-1;
			d = winner.getInitialDefense();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			while(sp<=4){
				sp = winner.getInitialSpeed() - r.nextInt(20)*5;
			}	
			h = 100 - sp-d-str-rng;
			break;
		case 12://vida en funcion de rango
			rng = -1;
			sp = winner.getInitialSpeed();
			d = winner.getInitialDefense();
			str = winner.getInitialStrength();
			while(rng<=0){
				rng = winner.getInitialRange() - r.nextInt(100);
			}	
			h = 100 - sp-d-str-rng;
			break;
		case 13://ataque en funcion de rango
			rng = -1;
			sp = winner.getInitialSpeed();
			d = winner.getInitialDefense();
			h = winner.getInitialHealth();
			while(rng<=0){
				rng = winner.getInitialRange() - r.nextInt(100);
			}	
			str = 100 - sp-h-d-rng;
			break;
		case 14://ataque en funcion de vida
			h = -1;
			sp = winner.getInitialSpeed();
			rng = winner.getInitialRange();
			d = winner.getInitialDefense();
			while(h <=0){
				h = winner.getInitialHealth() - r.nextInt(100);
			}
			str = 100 - sp-h-d-rng;
			break;
		case 15://ataque en funcion de velocidad
			sp=-1;
			d = winner.getInitialDefense();
			rng = winner.getInitialRange();
			h = winner.getInitialHealth();
			while(sp<=4){
				sp = winner.getInitialSpeed() - r.nextInt(20)*5;
			}	
			str = 100 - sp-h-d-rng;
			break;
		case 16://speed en funcion de rango
			rng = -1;
			str = winner.getInitialStrength();
			d = winner.getInitialDefense();
			h = winner.getInitialHealth();
			while(rng<=0){
				rng = winner.getInitialRange() - r.nextInt(100);
			}	
			sp = 100 - str-h-d-rng;
			break;
		case 17://speed en funcion de defensa
			d = -1;
			h = winner.getInitialHealth();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			while(d <=0){
				d = winner.getInitialDefense()- r.nextInt(100);
			}
			sp = 100 - str-h-d-rng;
			break;
		case 18://speed en funcion de vida
			h = -1;
			d = winner.getInitialDefense();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			while(h <=0){
				h = winner.getInitialHealth() - r.nextInt(100);
			}
			sp = 100 - str-h-d-rng;
			break;
		case 19://speed en funcion ataque
			str = -1;
			rng = winner.getInitialRange();
			d = winner.getInitialDefense();
			h = winner.getInitialHealth();
			while(str <=0){
				str = winner.getInitialStrength()- r.nextInt(100);
			}
			sp = 100 - str-h-d-rng;
			break;
		default:
			d = winner.getInitialDefense();
			h = winner.getInitialHealth();
			sp = winner.getInitialSpeed();
			rng = winner.getInitialRange();
			str = winner.getInitialStrength();
			break;
		}
		//int s = h+d+str+sp+rng;
		//System.out.println("Healt: "+h+ " defense: "+ d+ " strenght: "+ str+" speed: " + sp+ " range: "+rng  + " sum: "+ s);
		return new WarriorJ(winner.getName(),h,d,str,sp,rng);
	}
}
